package cn.iocoder.yudao.adminserver.franky.java8.Stream;

import cn.iocoder.yudao.adminserver.franky.java8.Person;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description:
 * @Author: franky
 * @CreateDate: 2021/8/25 4:46 下午
 * @Version: 1.0
 */
public class StreamStudy {
    public static void main(String[] args) {
        count();
        max();
        min();
        maxSalary();
        map();
        flatmap();
    }

    public static void count() {
        List<Integer> countList = Arrays.asList(7,6,5,4,12,54,23,88,77);
        Integer max = 6;
        long count = countList.stream().filter(t -> t>max).count();
        System.out.println(">>>>>> 大于" +max+"的元素有: " + count);
        List<Integer> maxList = countList.stream().filter(t -> t>max).collect(Collectors.toList());
        System.out.println(">>>>>> 大于" +max+"的元素: " + maxList);
    }

    public static void max() {
        List<Integer> countList = Arrays.asList(7,6,5,4,12,54,23,88,77);
        System.out.println("本次计算的数组: " + countList);
        Integer max = countList.stream().max(Comparator.naturalOrder()).orElse(null);
        System.out.println("最大的数：" + max);
    }

    public static void min() {
        List<Integer> minList = Arrays.asList(7,6,5,4,12,54,23,88,77);
        System.out.println("本次计算的数组: " + minList);
        Integer min = minList.stream().min(Integer::compareTo).orElse(null);
        System.out.println("最小的数：" + min);
    }

    public static void maxSalary() {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 1200, 12, "shanghai"));
        personList.add(new Person("tom", 1000, 12, "hong kong"));
        personList.add(new Person("franky", 32100, 12, "new york"));
        personList.add(new Person("sunny", 4200, 12, "paris"));
        personList.add(new Person("arlen", 800, 12, "french"));
        personList.add(new Person("tim", 6655, 12, "tokyo"));
        personList.add(new Person("李四", 5431, 12, "america"));


        Person person = personList.stream().max(Comparator.comparing(Person::getSalary)).orElse(null);
        System.out.println("工资最高的员工信息: " + JSONObject.toJSONString(person));
    }

    public static void map() {
        String[] strArr = {"Reeee", "frankY", "ToM"};
        List<String> upperList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(upperList);

    }

    public static void flatmap() {
        List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
        List<String> listNew = list.stream().flatMap(s -> {
            // 将每个元素转换成一个stream
            String[] split = s.split(",");
            return Arrays.stream(split);
        }).collect(Collectors.toList());

        System.out.println("处理前的集合：" + list);
        System.out.println("处理后的集合：" + listNew);

//       List<String> listMapNew = list.stream().map(s -> {
//           // 将每个元素转换成一个stream
//           String[] split = s.split(",");
//           Stream<String> s2 = Arrays.stream(split);
//           return s2;
//        }).collect(Collectors.toList());

        List<Integer> numbers1 = Arrays.asList(1,2,3);
        List<Integer> numbers2 = Arrays.asList(3,4);
        List<int[]> unionMap = numbers1.stream().flatMap(i -> numbers2.stream().map(j -> new int[]{i, j})).collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(unionMap));
    }
}
